/**
 * @Author: easten
 * @Created: 2024-07-17 15:04:01
 * @Description: 缓存工具类
 *
 */

import { IotDevice } from "_common/entity";

export const setDesignParams = (params: any) => {
  localStorage.setItem("designer", JSON.stringify(params));
};

export const getDesignParams = () => {
  return JSON.parse(localStorage.getItem("designer") || "{}");
};

export const setStorage = (key: string, value: string) => {
  localStorage.setItem(key, value);
  window.dispatchEvent(
    new CustomEvent("storage-changed", { detail: { key, value } })
  );
};

export const getStorage = (key: string) => {
  return localStorage.getItem(key);
};

export const removeStorage = (key: string) => {
  localStorage.removeItem(key);
  window.dispatchEvent(
    new CustomEvent("storage-changed", { detail: undefined })
  );
};

// 存储和处理设备数据
export const setDeviceDataCache = (data: IotDevice[]) => {
  setStorage("deviceData", JSON.stringify(data));
};

export const getDeviceDataCache = () => {
  return JSON.parse(getStorage("deviceData") || "[]");
};
